// ##############################
// ####  note:  bokeweilai event main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_event.h"
#include "bkwl_network.h"
#include "bkwl_mqtt.h"
#include "bkwl_bt.h"
#include "bkwl_uart.h"
#include "bkwl_camera.h"
#include "bkwl_http.h"
#include "dev_str.h"
#include "bkwl_lcd.h"
#include "bkwl_data.h"
#include "bkwl_protocol_jiwei.h"

#if BKWL_IOT_DOORBELL_UART_TEST == 1
extern uint8_t *uart_door_data_test;
extern uint32_t uart_door_data_len;
#endif

bkwl_err_t bkwl_event_init()
{
	bkwl_err_t ret = BK_OK;
	tprintf("bkwl_event_init entry\n");
	tprintf("\n\n");
	tprintf("================================================\n");
	tprintf("bkwl_event_init cur version=%s\n", BKWL_IOT_VER_CUR);
	tprintf("================================================\n");
	tprintf("\n\n");
	ret = rtos_create_thread(&bkwl_main->task.event,
							 BEKEN_DEFAULT_WORKER_PRIORITY,
							 "task.event",
							 (beken_thread_function_t)bkwl_event_main_handle,
							 1024 * 10,
							 NULL);

	if (ret != BK_OK)
	{
		tprintf("create media major thread fail\n");
		goto error;
	}
	tprintf("bkwl_event_init exit\n");
error:
	return ret;
}

bkwl_err_t bkwl_event_send_cmd(uint16_t cmd)
{
	bkwl_err_t ret = BK_OK;
	bkwl_msg_t msg;
	msg.param = 0;
	msg.cmd = cmd;
	msg.data = NULL;
	bkwl_event_send_cmd_msg(&msg);
	return ret;
}

bkwl_err_t bkwl_event_send_cmd_msg(bkwl_msg_t *msg)
{
	bkwl_err_t ret = BK_OK;

	if (bkwl_main->queue.msg != NULL)
	{
		ret = rtos_push_to_queue(&bkwl_main->queue.msg, msg, BEKEN_NO_WAIT);

		if (BK_OK != ret)
		{
			tprintf("#### twh bkwl_event_send_msg failed, cmd=%d \n", msg->cmd);
			return BK_FAIL;
		}

		return ret;
	}

	return ret;
}

bkwl_err_t bkwl_event_send_uart_door(bkwl_msg_t *msg)
{
	bkwl_err_t ret = BK_OK;
	// tprintf("bkwl_event_send_uart_door 1\n\n");
	if (bkwl_main->queue.uart_door != NULL)
	{
		// tprintf("bkwl_event_send_uart_door uart_door=0x%x\n\n", bkwl_main->queue.uart_door);
		ret = rtos_push_to_queue(&bkwl_main->queue.uart_door, msg, BEKEN_NO_WAIT);
		if (BK_OK != ret)
		{
			tprintf("#### twh bkwl_event_send_msg failed, cmd=%d \n", msg->cmd);
			return BK_FAIL;
		}
		// tprintf("bkwl_event_send_uart_door 3\n\n");
	}
	else
	{
		tprintf("#### bkwl_main->queue.uart_door==NULL \n\n");
	}
	return ret;
}

bkwl_err_t bkwl_event_board_init()
{
	bkwl_err_t ret = BK_OK;
	tprintf("bkwl_event_board_init entry\n");

	char *lcd_type_name = "h050iwv";
	media_ppi_t ppi = PPI_800X480;
	bkwl_lcd_init(lcd_type_name, ppi);

	uint32_t badurate = 9600;
	bkwl_uart_init(BKWL_IOT_DOORBELL_UART_ID, badurate);

	tprintf("bkwl_event_board_init 1\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
	bkwl_event_uart_init();
	tprintf("bkwl_event_board_init exit\n");
	return ret;
}
// int mqtt_client_example(const char *host_name, const char *username,
// 						const char *password, const char *topic);

// int mqtt_cmd_msg_send(char *topic, char *msg);

bkwl_err_t bkwl_event_message_handle(void)
{
	bkwl_err_t ret = BK_OK;
	bkwl_msg_t msg;

	tprintf("message_handle  entry\n");
	while (1)
	{

		tprintf("event wait main msg\n");
		if (bkwl_main->queue.msg != NULL)
		{
			ret = rtos_pop_from_queue(&bkwl_main->queue.msg, &msg, BEKEN_WAIT_FOREVER);

			if (kNoErr == ret)
			{
				tprintf("twh bkwl_event_message_handle get cmd=%d\n", msg.cmd);
				switch (msg.cmd)
				{
				case BKWL_WIFI_STATION_CONNECT:
				{
					tprintf("twh main event DBEVT_WIFI_STATION_CONNECT\n");
					// char *ssid = "Bokeweilai";
					// char *key = "BKWL666666";

					// char *ssid = "belon-2.4";
					// char *key = "belon83740372";

					// char *ssid = "BELON";
					// char *key = "belon83740372";

					// char *ssid = "wifi-2.4G";
					// char *key = "12345678";

#if 0
				// char *ssid = "wifi-2.4G";
				// char *key = "12345678";

				char *ssid = "rbq13";
				char *key = "12345678";

				bkwl_network_wifi_sta_connect(ssid, key);
#else
					doorbell_boarding_info_t *doorbell_boarding_info = (doorbell_boarding_info_t *)msg.param;

					strcpy(bkwl_main->bt.ssid, doorbell_boarding_info->boarding_info.ssid_value);
					strcpy(bkwl_main->bt.password, doorbell_boarding_info->boarding_info.password_value);

					bkwl_network_wifi_sta_connect(bkwl_main->bt.ssid,
												  bkwl_main->bt.password);

					// bkwl_network_wifi_sta_connect(doorbell_boarding_info->boarding_info.ssid_value,
					// 							  doorbell_boarding_info->boarding_info.password_value);
#endif
				}
				break;
				case BKWL_WIFI_STATION_BKWL_CONNECT:
				{
					tprintf("twh main event BKWL_WIFI_STATION_BKWL_CONNECT\n");
					doorbell_boarding_info_t *doorbell_boarding_info = (doorbell_boarding_info_t *)msg.param;
					tprintf("twh main get bt pair data:\n%s\n", doorbell_boarding_info->boarding_info.password_value);
					bkwl_get_bt_pair_data(doorbell_boarding_info->boarding_info.password_value);
					if (strlen(bkwl_main->bt.ssid) > 0 && strlen(bkwl_main->bt.password) > 0)
					{
						bkwl_network_wifi_sta_connect(bkwl_main->bt.ssid,
													  bkwl_main->bt.password);
					}

					bkwl_send_bt_pair_ack();
					bkwl_network_wifi_sta_connect(doorbell_boarding_info->boarding_info.ssid_value,
												  doorbell_boarding_info->boarding_info.password_value);
					break;
				}
				case BKWL_WIFI_DATA_CONNECT:
				{
					tprintf("twh main event DBEVT_WIFI_STATION_CONNECT\n");
					// char *ssid = "Bokeweilai";
					// char *key = "BKWL666666";

					// char *ssid = "belon-2.4";
					// char *key = "belon83740372";

					// char *ssid = "BELON";
					// char *key = "belon83740372";

					// char *ssid = "wifi-2.4G";
					// char *key = "12345678";

#if 1
					// char *ssid = "wifi-2.4G";
					// char *key = "12345678";

					// char *ssid = "rbq13";
					// char *key = "12345678";
					char *ssid = "Bokeweilai";
					char *key = "BKWL666666";

					bkwl_network_wifi_sta_connect(ssid, key);
#else
					doorbell_boarding_info_t *doorbell_boarding_info = (doorbell_boarding_info_t *)msg.param;
					bkwl_network_wifi_sta_connect(doorbell_boarding_info->boarding_info.ssid_value,
												  doorbell_boarding_info->boarding_info.password_value);
#endif
				}
				break;
				case BKWL_WIFI_STATION_CONNECTED:
				{
					tprintf("twh main event BKWL_WIFI_STATION_CONNECTED\n");
					netif_ip4_config_t ip4_config;
					extern uint32_t uap_ip_is_start(void);
					os_memset(&ip4_config, 0x0, sizeof(netif_ip4_config_t));
					bk_netif_get_ip4_config(NETIF_IF_AP, &ip4_config);
					if (uap_ip_is_start())
						bk_netif_get_ip4_config(NETIF_IF_AP, &ip4_config);
					else
						bk_netif_get_ip4_config(NETIF_IF_STA, &ip4_config);
					tprintf("\n\n\nip: %s\n", ip4_config.ip);

#if 1
					//	bkwl_event_send_cmd(BKWL_CMD_INIT_MQTT);
					msg.cmd = BKWL_CMD_PROTOCOL_MQTT_INIT;
					msg.param = 0;
					msg.data = 0;
					bkwl_event_send_uart_door(&msg);

#else
					char *host_name = "120.79.69.20";
					char *username = "tuiyun";
					char *password = "tuiyun123";
					char *topic = "/bkwl/server/data/sn_123";
					bkwl_mqtt_client_init(host_name, username, password, topic);
#endif
				}
				break;
				case BKWL_CMD_MQTT_SEND_DATA:
				{
					// tprintf("twh main event BKWL_CMD_MQTT_SEND_DATA\n");
#if 0
				char *topic_send = "/bkwl/client/data";
				bkwl_mqtt_cmd_msg_send(topic_send, "aaaaaaa");
#else

#if 0
				bkwl_mqtt_sibo_heartbeat_send();
				// 属性发送
				char *type = "01";
				char *data = "0.95";
				bkwl_mqtt_sibo_property_send(type, data);

				char *user = "01";
				char *utype = "1";
				// 事件上报
				//  {"ver":"1.0.0","act":"0004","type":"01","data":"","user":"01","utype":"1","seq_no":"xxxxxxxxxx"} 
				bkwl_mqtt_sibo_notify_send(type, user, data, utype);

				// {"ver":"1.0.0","act":"0005","sta":"00","seq_no":"xxxxxxxxxx"} 
				// char *name = "sta";
				// char *data1 = "05";
				// bkwl_mqtt_sibo_notify_reply_send(name, data1);
#endif

#endif

					break;
				}
				case BKWL_CMD_BOARD_INIT:
				{
					// bkwl_bluetooth_init();
					bkwl_event_board_init();
#if BKWL_IOT_DOORBELL_EVENT_UART_DOOR_HANDEL == 1
					ret = rtos_create_thread(&bkwl_main->task.uart_msg,
											 BEKEN_DEFAULT_WORKER_PRIORITY,
											 "task.uart_msg",
											 (beken_thread_function_t)bkwl_event_uart_door_handle,
											 1024 * 10,
											 NULL);
#endif
					break;
				}
				case BKWL_CMD_CAMERA_INIT:
				{
					tprintf("twh main event BKWL_CMD_CAMERA_INIT\n");
					bkwl_camera_uvc_init();
					break;
				}
				case BKWL_CMD_ENABLED_BT:
				{
					tprintf("twh main event BKWL_CMD_ENABLED_BT\n");
					bkwl_bluetooth_init();
					break;
				}
				case BKWL_CMD_HTTP_POST_IMAGE:
				{
					tprintf("twh main event BKWL_CMD_HTTP_POST_IMAGE\n");
#if 0
				char *url_upload = "https://m.yo-jia.cn/api/wx/upload/upload.php";
				char *picture_data = "aaaaa";
				int picture_length = 5;
				bkwl_http_upload_picture(url_upload, picture_data, picture_length);

				char *downfile = "https://www.baidu.com/img/flexible/logo/pc/result.png";
				bkwl_http_download_file(downfile);
#endif

#if 0
				// char *url_get_time = "http://m.yo-jia.cn/api/wx/mqtt/getTime.php";
				// bkwl_http_get(url_get_time);
				// char *url_post_login = "http://m.yo-jia.cn/api/wx/applet/phone/login";
				char *url_post_data = "http://m.yo-jia.cn/api/wx/upload/upload.php";
				// char *url_post_data = "http://192.168.1.187:7303/upload/upload.php";
				uint8_t *post_data = (uint8_t *)"aaaaaaa";
				uint32_t data_len = strlen((char *)post_data);
				char *device_name = "D_bFbWajDKqfx4C4am";
				// bkwl_http_post_formdata(url_post_data, post_data, data_len);
				bkwl_http_post_filedata(url_post_data, post_data, data_len, device_name);
				// char *url_post_data = "http://m.yo-jia.cn/api/wx/test/test.php";
				// char *post_data = "{}";
				// uint32_t data_len = strlen(post_data);
				// tprintf("twh main event 1\n");
				// bkwl_http_post(url_post_data, post_data, data_len);
				// char *downfile = "https://www.baidu.com/img/flexible/logo/pc/result.png";
				// bkwl_http_get(downfile);
#endif

					//	bkwl_mqtt_sibo_upload_image();
					//	bkwl_doorbell_post_image();
					break;
				}
				case BKWL_CMD_INIT_MQTT:
				{

#if 1
					// bkwl_doorbell_init_mqtt();
#else

					// char *host_name = "120.79.69.20";
					// char *username = "tuiyun";
					// char *password = "tuiyun123";
					// char *topic = "/bkwl/server/data/sn_123";

					strcpy(bkwl_main->mqtt.host, "m.yo-jia.cn");
					tprintf("default host=%s\n", bkwl_main->mqtt.host);
					//	strcpy(bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET, "/sys/P_1jOMUMWtsEeP8PLP/D_OSdzmGueXwlln1cY/service/property/set");
					strcpy(bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET, "device/P_1jOMUMWtsEeP8PLP/D_OSdzmGueXwlln1cY/service/property/set");
					tprintf("default topic=%s\n", bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET);
					strcpy(bkwl_main->mqtt.device_name, "D_OSdzmGueXwlln1cY");
					tprintf("default device_name=%s\n", bkwl_main->mqtt.device_name);
					strcpy(bkwl_main->mqtt.product_key, "P_1jOMUMWtsEeP8PLP");
					tprintf("default product_key=%s\n", bkwl_main->mqtt.product_key);
					strcpy(bkwl_main->mqtt.device_secret, "gXd9pmCavN8VP2qZVguPjuQZzaPtD0R0");
					tprintf("default device_secret=%s\n", bkwl_main->mqtt.device_secret);
					tprintf("bkwl_doorbell_init_mqtt product_key=%s\n", bkwl_main->mqtt.product_key);
					bkwl_data_mqtt_init(bkwl_main->mqtt.device_name, bkwl_main->mqtt.product_key);
					bkwl_mqtt_client_init(bkwl_main->mqtt.host, bkwl_main->mqtt.device_name, bkwl_main->mqtt.device_secret, bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET);

#endif
					break;
				}
				case BKWL_CMD_LCD_DISPLAY:
				{
					// bkwl_lcd_display_red();
					break;
				}
				case BKWL_CMD_AGORA_INIT:
				{
					// bkwl_agora_init();
					// bkwl_agora_config();
					break;
				}
				case BKWL_CMD_UART_WRITE:
				{
					// tprintf("BKWL_CMD_UART_WRITE 1\n");
					// char data_buf[20] = {0};
					// // bkwl_uart_write(BKWL_IOT_DOORBELL_UART_ID, p_write_buf, 10);
					// strcpy(data_buf, "Helloworld\r\n");
					// bkwl_uart_write(BKWL_IOT_DOORBELL_UART_ID, (uint8_t *)data_buf, strlen(data_buf));
					// tprintf("BKWL_CMD_UART_WRITE 2\n");

					break;
				}
				}
			}
		}
	}
}

#if BKWL_IOT_DOORBELL_EVENT_UART_DOOR_HANDEL == 1
bkwl_err_t bkwl_event_uart_door_handle(void)
{
	bkwl_err_t ret = BK_OK;
	bkwl_msg_t msg;

	tprintf("bkwl_event_uart_door_handle  entry\n");
	while (1)
	{

		if (bkwl_main->queue.uart_door != NULL)
		{
			// tprintf("twh bkwl_event_uart_door_handle wait uart door msg\n");
			ret = rtos_pop_from_queue(&bkwl_main->queue.uart_door, &msg, BEKEN_WAIT_FOREVER);
			if (kNoErr == ret)
			{
				//	tprintf("twh bkwl_event_uart_door_handle get cmd=%d\n", msg.cmd);
				switch (msg.cmd)
				{
				case BKWL_CMD_DOOR_START_WIFI_PAIR:
				{
					bkwl_door_wifi_pair_t *bkwl_door_wifi_pair = (bkwl_door_wifi_pair_t *)msg.data;
					//  bkwl_door_wifi_pair->type = data[5];
					//  bkwl_door_wifi_pair->notify = data[7];
					tprintf("main get BKWL_CMD_START_WIFI_PAIR \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_PAIR_FAIL, msg.param);
					free(bkwl_door_wifi_pair);
					break;
				}
				case BKWL_CMD_DOOR_MESSAGE:
				{
					bkwl_door_message_t *bkwl_door_message = (bkwl_door_message_t *)msg.data;
					tprintf("main get BKWL_CMD_DOOR_MESSAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					free(bkwl_door_message);
					break;
				}
				case BKWL_CMD_DOOR_POWER_OFF:
				{
					bkwl_door_power_off_t *bkwl_door_power_off = (bkwl_door_power_off_t *)msg.data;
					tprintf("main get BKWL_CMD_DOOR_POWER_OFF \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					free(bkwl_door_power_off);
					break;
				}
				case BKWL_CMD_DOOR_CLEAN_DATA:
				{
					tprintf("main get BKWL_CMD_DOOR_CLEAN_DATA \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_DEVICE_INFO:
				{
					tprintf("main get BKWL_CMD_DOOR_GET_DEVICE_INFO \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_UPGRADE:
				{
					tprintf("main get BKWL_CMD_DOOR_UPGRADE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_PRODUCT_TEST:
				{
					tprintf("main get BKWL_CMD_DOOR_PRODUCT_TEST \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_NET_TIME:
				{
					tprintf("main get BKWL_CMD_DOOR_GET_NET_TIME \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_ACK_REMOTE_UNLOCK:
				{
					tprintf("main get BKWL_CMD_DOOR_ACK_REMOTE_UNLOCK \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_UPLOAD_IMAGE:
				{
					tprintf("main get BKWL_CMD_DOOR_UPLOAD_IMAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_USER_VERIFY:
				{
					tprintf("main get BKWL_CMD_DOOR_USER_VERIFY \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_START_UPLOAD_IMAGE:
				{
					tprintf("main get BKWL_CMD_DOOR_START_UPLOAD_IMAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_VIDEO_STATUS_NOTIFY:
				{
					tprintf("main get BKWL_CMD_DOOR_VIDEO_STATUS_NOTIFY \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_VIDEO_PARA_SETUP:
				{
					tprintf("main get BKWL_CMD_DOOR_VIDEO_PARA_SETUP \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_STATUS_REPORT:
				{
					tprintf("main get BKWL_CMD_DOOR_STATUS_REPORT \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_STOP_WIFI_PAIR:
				{
					tprintf("main get BKWL_CMD_DOOR_STOP_WIFI_PAIR \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_CACHE_MESSAGE:
				{
					tprintf("main get BKWL_CMD_DOOR_CACHE_MESSAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_DEVICE_FUNC:
				{
					tprintf("main get BKWL_CMD_DOOR_DEVICE_FUNC \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_LOOK_SETUP:
				{
					tprintf("main get BKWL_CMD_DOOR_LOOK_SETUP \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_VERSION:
				{
					tprintf("main get BKWL_CMD_DOOR_GET_VERSION \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_WEATHER:
				{
					tprintf("main get BKWL_CMD_DOOR_GET_WEATHER \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_REGION:
				{
					tprintf("main get BKWL_CMD_DOOR_GET_REGION \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				}
			}
		}
		else
		{
			rtos_delay_milliseconds(1000);
		}
	}
	return ret;
}
#endif

bkwl_err_t bkwl_event_main_handle(void *para)
{
	bkwl_err_t ret = BK_OK;
	uint32_t num = 1;
	tprintf("main_handle entry\n");
	ret = rtos_create_thread(&bkwl_main->task.msg,
							 BEKEN_DEFAULT_WORKER_PRIORITY,
							 "task.msg",
							 (beken_thread_function_t)bkwl_event_message_handle,
							 1024 * 10,
							 NULL);

	tprintf("main_handle create task msg\n");
	while (1)
	{

		if (num == 1)
		{
			bkwl_event_send_cmd(BKWL_CMD_BOARD_INIT);
		}

#if 0
		if (num == 2)
		{
			bkwl_event_send_cmd(BKWL_WIFI_DATA_CONNECT);
		}
#endif
#if 0
		if (num % 14 == 0)
		{
			bkwl_event_send_cmd(BKWL_CMD_UART_WRITE);
		}
#endif

#if 0
		if (num == 2)
		{
			bkwl_event_send_cmd(BKWL_CMD_CAMERA_INIT);
		}
#endif
#if 0
		if (num % 15 == 0)
		{
			bkwl_event_send_cmd(BKWL_CMD_HTTP_POST_IMAGE);
		}
#endif
#if 0
		if (num % 16 == 0)
		{
			bkwl_event_send_cmd(BKWL_CMD_AGORA_INIT);
		}
#endif

		if (num % 5 == 0)
		{
			if (bkwl_main->status.mqtt_connect == 1)
			{
				if (strlen(bkwl_main->bt.token) > 2)
				{
					tprintf("send pair topic token: %s\n", bkwl_main->bt.token);
					// bkwl_mqtt_sibo_pair_net(bkwl_main->bt.token);
					memset(bkwl_main->bt.token, 0, 64);
				}
			}
		}

#if 0
		if (num % 5 == 0)
		{
		
			bkwl_event_send_cmd(BKWL_CMD_LCD_DISPLAY);
		}
#endif

#if 0
		if (num % 35 == 0)
		{
			if (bkwl_main->status.mqtt_connect == 1)
				bkwl_event_send_cmd(BKWL_CMD_MQTT_SEND_DATA);
		}
#endif

#if 1
		if (num == 10)
		{
			bkwl_common_test_open_door();
		}
#endif

		// if (num % 60 * 30 == 0)
		// {
		// 	tprintf("twh bkwl_event_main_handle num=%d\n", num);
		// }

		if (num > 0xffff)
			num = 0;
		num++;
		rtos_delay_milliseconds(1000);
	}

	return ret;
}
#define BKWL_UART2_BUF_LEN 128
bkwl_err_t bkwl_event_handle_uart_door(void)
{
	bkwl_err_t ret = BK_OK;
	uint32_t read_quene_len = 0;
	uint32_t deal_len = 0;
	int len = 0;
	tprintf("uart_door entry\n\n\n\n");
	uint8_t *uart_data = (uint8_t *)os_malloc(BKWL_UART2_BUF_LEN);

	uint8_t *read_quene_data = (uint8_t *)os_malloc(BKWL_UART2_BUF_LEN);
	os_memset(uart_data, 0, BKWL_UART2_BUF_LEN);
	os_memset(read_quene_data, 0, BKWL_UART2_BUF_LEN);
	bk_set_printf_sync(true);
	while (1)
	{
// tprintf("bkwl_event_handle_uart_door wait data\n\n");
#if BKWL_IOT_DOORBELL_UART_TEST == 0
		len = bk_uart_read_bytes(BKWL_IOT_DOORBELL_UART_ID, uart_data, BKWL_UART2_BUF_LEN, BEKEN_WAIT_FOREVER);
#else
		if (uart_door_data_test != NULL && uart_door_data_len > 0)
		{
			tprintf("uart_door uart_door_data_len=%d\n\n\n", uart_door_data_len);
			memcpy(uart_data, uart_door_data_test, uart_door_data_len);
			len = uart_door_data_len;
			uart_door_data_len = 0;
		}
#endif

		if (len < 0)
		{
			tprintf("uart read failed, ret:-0x%x\r\n", -len);
			continue;
		}
		// else
		// {
		// 	tprintf("bkwl_event_handle_uart_door get len=%d\n", len);
		// 	for (int i = 0; i < len; i++)
		// 	{
		// 		tprintf("%x ", uart_data[i]);
		// 	}
		// 	tprintf("\r\n");
		// }
		ringbuffer_write(&bkwl_main->ringbuffer.uart_door, uart_data, len);
		// 从缓冲区读数据
		read_quene_len = ringbuffer_check_buf(&bkwl_main->ringbuffer.uart_door, read_quene_data, ringbuffer_get_size_used(&bkwl_main->ringbuffer.uart_door));
		// tprintf("bkwl_event_handle_uart_door read_quene_len=%d: \n", read_quene_len);

		if (read_quene_len >= 10)
		{
			//	tprintf("ringbuffer_check_buf start: \n", len);
			for (int i = 0; i < read_quene_len; i++)
			{
				tprintf("%02x ", read_quene_data[i]);
			}
			tprintf("\n");
			deal_len = bkwl_protocol_jiwei_get_cmd_handle(read_quene_data, read_quene_len);
			if (deal_len > 0)
			{
				//	tprintf("ringbuffer_check_buf get deal_len=%d \n", deal_len);
				ringbuffer_set_read_end(&bkwl_main->ringbuffer.uart_door, deal_len);
				// read_quene_len = ringbuffer_check_buf(&bkwl_main->ringbuffer.uart_door, read_quene_data, ringbuffer_get_size_used(&bkwl_main->ringbuffer.uart_door));
				// for (int i = 0; i < read_quene_len; i++)
				// {
				// 	tprintf("%x ", read_quene_data[i]);
				// }
				// tprintf("\n\n\n");
			}
		}

		os_memset(uart_data, 0, BKWL_UART2_BUF_LEN);

#if BKWL_IOT_DOORBELL_UART_TEST == 1
		rtos_delay_milliseconds(500);
#endif
	}

	tprintf("bkwl_event_handle_uart_door exit=============\n\n\n\n");
	return ret;
}
bkwl_err_t bkwl_event_uart_init()
{
	bkwl_err_t ret = BKWL_OK;
	tprintf("bkwl_event_uart_init entry\n");

	ret = rtos_create_thread(&bkwl_main->task.uart_door,
							 BEKEN_DEFAULT_WORKER_PRIORITY,
							 "task.uart_door",
							 (beken_thread_function_t)bkwl_event_handle_uart_door,
							 1024 * 10,
							 NULL);
	tprintf("bkwl_event_uart_init exit\n");
	return ret;
}